package com.jocata.hibernate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.jocata.MatchingTwoStrings;


public class Main {

	public static void main(String[] args) {
		List<CustomerPrincipal> cusPrinlst = null;
		List<WatchlistName> watchlstNames = null;
		
		// Read
		System.out.println("******* READ *******");
		cusPrinlst = HibernateUtil.getCustomerPrincipalList();
		watchlstNames = HibernateUtil.getWatchListNamesList();
		List<String> noiseList = null;
		noiseList = HibernateUtil.getCatalogList();
		List<String> aliasesList = HibernateUtil.getAllAliases();
		HashMap<Integer, ArrayList<String>> aliasGroups = HibernateUtil.getAliasListsByType();
		
		CustomerPrincipal custPrnc = null;
		WatchlistName watchListName = null;
		MatchResult matchRes = null;
		String cusPrncName = null;
		String watchLstName = null;
		float res = 0 ;
		int count = 0;
		MatchingTwoStrings mts = new MatchingTwoStrings();
		if(cusPrinlst != null && watchlstNames != null){
			for(CustomerPrincipal cs : cusPrinlst){
				custPrnc = cs;
				if( custPrnc != null){
					if(custPrnc.getEntityName() != null && custPrnc.getEntityName().length() > 0){
						cusPrncName = custPrnc.getEntityName();
					}else{
						if(custPrnc.getFirstname() != null){
							cusPrncName = custPrnc.getFirstname();
						}
						if(custPrnc.getMiddlename() != null){
							if(cusPrncName != null && cusPrncName.length() > 0)
							cusPrncName = cusPrncName+" "+custPrnc.getMiddlename();
						}
						if(custPrnc.getLastname() != null){
							if(cusPrncName != null && cusPrncName.length() > 0)
							cusPrncName = cusPrncName+" "+custPrnc.getLastname();
						}
					}
						for(WatchlistName wln : watchlstNames){
							watchListName = wln;
							if(watchListName != null){
								if(watchListName.getEntityName() != null && watchListName.getEntityName().length() > 0){
									watchLstName = watchListName.getEntityName();
								}else{
									if(watchListName.getFirstName() != null){
										watchLstName = watchListName.getFirstName();
									}
									if(watchListName.getMiddleName() != null){
										if(watchLstName != null && watchLstName.length() > 0)
											watchLstName = watchLstName+" "+watchListName.getMiddleName();
									}
									if(watchListName.getLastName() != null){
										if(watchLstName != null && watchLstName.length() > 0)
											watchLstName = watchLstName+" "+watchListName.getLastName();
									}
								}
									if(cusPrncName != null && watchLstName != null){
										if(custPrnc.getEntityName() != null && custPrnc.getEntityName().length() > 0){
											res = mts.getMatchPercentage(watchLstName,cusPrncName,true);
										}else{
											res = mts.getMatchPercentage(watchLstName,cusPrncName,false);
										}
									}
									if(res != 0 && res > 85){
										matchRes = new MatchResult();
										if(custPrnc != null && (custPrnc.getCpId() != 0 && custPrnc.getCpId() > 0))
											matchRes.setCpId(custPrnc.getCpId());
										if(watchListName != null && (watchListName.getWatchlistId() != 0 && watchListName.getWatchlistId() > 0))
											matchRes.setWatchlistId(watchListName.getWatchlistId());
										matchRes.setScore(res);
										HibernateUtil.saveMatchResult(matchRes);
									}
										
									System.out.println("result : "+res);
							}
							if(watchListName.getMiddleName() != null){
								if(watchLstName != null && watchLstName.length() > 0)
									watchLstName = watchLstName+" "+watchListName.getMiddleName();
							}
							if(watchListName.getLastName() != null){
								if(watchLstName != null && watchLstName.length() > 0)
									watchLstName = watchLstName+" "+watchListName.getLastName();
							}
							if(cusPrncName != null && watchLstName != null){
								//res = mts.getMatchPercentage(cusPrncName, watchLstName);
								mts.getMatchPercentageFromDB(cusPrncName, watchLstName, noiseList, aliasesList, aliasGroups);
							}
							System.out.println("result : "+res);
						}
				}
			}
		}
	}
}
